/**
 * The scores package of the application.
 */
package fr.umlv.m2.android.quizz.scores;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import fr.umlv.m2.android.quizz.R;
import fr.umlv.m2.android.quizz.database.Level;
import fr.umlv.m2.android.quizz.database.LevelDb;
import fr.umlv.m2.android.quizz.database.Score;
import fr.umlv.m2.android.quizz.database.ScoreDb;
import fr.umlv.m2.android.quizz.database.Theme;
import fr.umlv.m2.android.quizz.database.UserDb;

/**
 * This class defines multiplayer scores activity according level.
 * 
 * @author Bambouele Bertrand.
 * @author Viravout Alexandre.
 */
public class ScoresMultiplayerByLevelActivity extends Activity {
	
	/**
	 * The best tournament rank text.
	 */
	private TextView bestTournamentRankText;
	/**
	 * The best tournament rank player.
	 */
	private TextView bestTournamentRankPlayer;
	/**
	 * The best number of good answer text.
	 */
	private TextView bestGoodAnswerText;
	/**
	 * The best number of good answer player.
	 */
	private TextView bestGoodAnswerPlayer;
	/**
	 * The best number of bad answer text.
	 */
	private TextView bestBadAnswerText;
	/**
	 * The best number of bad answer player.
	 */
	private TextView bestBadAnswerPlayer;
	/**
	 * The best percentage of success text.
	 */
	private TextView bestSuccessPercentageText;
	/**
	 * The best percentage of success player.
	 */
	private TextView bestSuccessPercentagePlayer;
	
	/**
	 * The last game player.
	 */
	private TextView lastGamePlayer;
	/**
	 * The last tournament rank text.
	 */
	private TextView tournamentRankText;
	/**
	 * The last number of good answer text.
	 */
	private TextView goodAnswerText;
	/**
	 * The last number of bad answer text. 
	 */
	private TextView badAnswerText;
	/**
	 * The last percentage of success text.
	 */
	private TextView successPercentageText;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.scores_multiplayer_by_level);
		
		Intent intent = this.getIntent();
	    final Bundle bundle = intent.getExtras();
	    final long levelId = bundle.getLong("levelId");
		
		this.bestTournamentRankText = (TextView)findViewById(R.id.scoresMultiplayerBestTournamentRankText);
		this.bestTournamentRankPlayer = (TextView)findViewById(R.id.scoresMultiplayerBestTournamentRankPlayer);
		this.bestGoodAnswerText = (TextView)findViewById(R.id.scoresMultiplayerBestGoodAnswerText);
		this.bestGoodAnswerPlayer = (TextView)findViewById(R.id.scoresMultiplayerBestGoodAnswerPlayer);
		this.bestBadAnswerText = (TextView)findViewById(R.id.scoresMultiplayerBestBadAnswerText);
		this.bestBadAnswerPlayer = (TextView)findViewById(R.id.scoresMultiplayerBestBadAnswerPlayer);
		this.bestSuccessPercentageText = (TextView)findViewById(R.id.scoresMultiplayerBestSuccessPercentageText);
		this.bestSuccessPercentagePlayer = (TextView)findViewById(R.id.scoresMultiplayerBestSuccessPercentagePlayer);
		
		this.lastGamePlayer = (TextView)findViewById(R.id.scoresMultiplayerLastGamePlayer);
		this.tournamentRankText = (TextView)findViewById(R.id.scoresMultiplayerTournamentRankText);
		this.goodAnswerText = (TextView)findViewById(R.id.scoresMultiplayerGoodAnswerText);
		this.badAnswerText = (TextView)findViewById(R.id.scoresMultiplayerBadAnswerText);
		this.successPercentageText = (TextView)findViewById(R.id.scoresMultiplayerSuccessPercentageText);
		
		LevelDb levelDb = new LevelDb(this);
		levelDb.open();
		Level level = levelDb.getLevelWithId(levelId);
		levelDb.close();
		
		this.setTitle(
				this.getResources().getString(R.string.scoresMultiplayerBarTitle) +
				" " +
				level.getName().toLowerCase()
		);

		ScoreDb scoreDb = new ScoreDb(this);
		scoreDb.open();

		if(scoreDb.getNbScore(levelId, Theme.NO_THEME) > 4) {
			List<Score> scores = scoreDb.getMultiplayerScores(levelId, Theme.NO_THEME);
			final UserDb userDb = new UserDb(this);
			userDb.open();
			
			Score score = scores.get(Score.TOURNAMENT_RANK_ID);
			if(score.getTournamentRank() != 0) {				
				this.bestTournamentRankText.setText(String.valueOf(score.getTournamentRank()));
				this.setUser(score.getUserId(), this.bestTournamentRankPlayer);
			}
			
			score = scores.get(Score.GOOD_ANSWER_ID);
			if(score.getGoodAnswer() != 0) {				
				this.bestGoodAnswerText.setText(String.valueOf(score.getGoodAnswer()));
				this.bestGoodAnswerPlayer.setText(userDb.getUserWithId(score.getUserId()).getName());
			}
			
			score = scores.get(Score.BAD_ANSWER_ID);
			if(score.getBadAnswer() != 0) {				
				this.bestBadAnswerText.setText(String.valueOf(score.getBadAnswer()));
				this.bestBadAnswerPlayer.setText(userDb.getUserWithId(score.getUserId()).getName());
			}
			
			score = scores.get(Score.SUCCESS_PERCENTAGE_ID);
			if(score.getSuccessPercentage() != 0) {				
				this.bestSuccessPercentageText.setText(String.valueOf(score.getSuccessPercentage()));
				this.bestSuccessPercentagePlayer.setText(userDb.getUserWithId(score.getUserId()).getName());
			}
			
			score = scores.get(Score.LAST_GAME_ID);
			this.lastGamePlayer.setText(userDb.getUserWithId(score.getUserId()).getName());
			this.tournamentRankText.setText(String.valueOf(score.getTournamentRank()));
			this.goodAnswerText.setText(String.valueOf(score.getGoodAnswer()));
			this.badAnswerText.setText(String.valueOf(score.getBadAnswer()));
			this.successPercentageText.setText(String.valueOf(score.getSuccessPercentage()));
			
			userDb.close();
		}
		
		scoreDb.close();
	}
	
	private void setUser(long userId, TextView textView) {
		if(userId != 0) {
			final UserDb userDb = new UserDb(this);
			userDb.open();
			
			textView.setText(userDb.getUserWithId(userId).getName());
			
			userDb.close();
		}
	}
}
